<?php

namespace app\api\model\plus\discount;

use app\common\model\plus\discount\Active as ActiveModel;
use app\common\model\plus\discount\DiscountGitProduct;
use app\common\service\product\BaseProductService;


class Active extends ActiveModel
{
    public function checkOrderTime($data)
    {
        $result = ['code' => 0];
        if ($data['start_time'] > time()) {
            $result = ['code' => 10, 10 => '该活动未开始'];
        }
        if ($data['end_time'] < time()) {
            $result = ['code' => 20, 20 => '该活动已结束'];
        }
        $now_start_time = strtotime(date('Y-m-d') . ' ' . $data['day_start_time']);
        $now_end_time = strtotime(date('Y-m-d') . ' ' . $data['day_end_time']);
        if ($now_start_time > time()) {
            $result = ['code' => 30, 30 => '该活动今天未开始'];
        }
        if ($now_end_time < time()) {
            $result = ['code' => 40, 40 => '该活动今天已结束'];
        }
        return $result;
    }

    /**
     * 取最近要结束的一条记录
     */
    public static function getActive()
    {
        return (new static())->where('start_time', '<', time())
            ->where('end_time', '>', time())
            ->where('status', '=', 1)
            ->where('is_delete', '=', 0)
            ->order(['sort' => 'asc', 'create_time' => 'asc'])
            ->find();
    }

    /**
     * 获取优惠活动
     */
    public function activityList()
    {
        return $this->where('end_time', '>', time())
//            ->where('start_time', '<=', time())
            ->where('status', '=', 1)
            ->where('is_delete', '=', 0)
            ->order(['sort' => 'asc', 'start_time' => 'asc'])
            ->select();
    }

    /**
     * 获取优惠商品
     */
    public static function getProductdiscountDetail($product_id)
    {
        $active = (new static())->alias('a')
            ->where('a.end_time', '>', time())
            ->where('a.status', '=', 1)
            ->where('a.is_delete', '=', 0)
            ->join('discount_product s', 's.discount_activity_id=a.discount_activity_id')
            ->where('product_id', '=', $product_id)->where('s.is_delete', '=', 0)->where('s.status', '=', 10)
            ->field('s.*')
            ->find();
        $detail = "";
        if ($active) {
            $detail = (new Product())->getDiscountDetail($active['discount_product_id']);
            $detail['active'] = self::detailWithTrans($active['discount_activity_id']);
            $detail['specData'] = BaseProductService::getSpecData($detail['product']);

            //活动类型（1=按件数打折，2=首件优惠，按几件起购，3=满赠）
            $detail['gift_product'] =  [];
            if($detail['activity_type'] == 3){
                $detail['gift_product'] = self::getGitProduct($product_id,$detail['supplier']['shop_supplier_id']);
            }
        }


        return $detail;
    }


    public static function getGitProduct($product_id,$shop_supplier_id){
        $map = [];
        $map[] = ['product_id','=',$product_id];
        $map[] = ['stock','>',0];
        $map[] = ['state','=',10];
        $map[] = ['is_delete','=',0];
        $map[] = ['audit_status','=',10];
        $map[] = ['shop_supplier_id','=',$shop_supplier_id];
        return (new DiscountGitProduct()) -> with(['file']) -> where($map) -> find();
    }
}
